package borge.web.teamspirit.client.page.team.widgets;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import borge.web.teamspirit.client.data.Event;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.ListDataWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Hyperlink;

public class ListEventsWidget extends ListDataWidget implements ClickHandler {

	// List events in format like this:
	/* All events are links
	 * New events:
	 * New forum post by Andreas Borglin
	 * New match created
	 * New serie created
	 * New comment in match
	 * New comment in serie
	 * New player joined
	 * New news posted
	 * Match won/lost/tie
	 * Finished 1st/nth in serie
	 */
	
	public ListEventsWidget(WidgetEventHandler aEventHandler, List<PersistentData> aEvents){
		super(aEventHandler, aEvents);
		
		// fake a list
		iData = new ArrayList<PersistentData>();
		Event event = new Event();
		event.setText("New event created!");
		event.setDate(new Date());
		iData.add(event);
		
		createDataTable();
		initWidget(iPanel);
	}
	
	public void onClick(ClickEvent e){
		//Window.alert("New event");
		Hyperlink link = (Hyperlink)e.getSource();
		//Window.alert("New event: " + link.getText());
		
		// TODO: do we need to fix history here??
		
		// call server, fetch object from key in event
		// get widget from object and display da shit!
		
		// set iShowIndex here
		//iShowIndex = 0;
//		postEvent(WidgetEventType.SHOW_EVENT);
	}
	
	@Override
	protected void createDataTable() {
		
		// reset here?
		
		// fake list of events
		Hyperlink link = new Hyperlink("Text", "DisplayEvent");
		link.addClickHandler(this);
		
		Hyperlink link2 = new Hyperlink("Text2", "DisplayEvent");
		link2.addClickHandler(this);
		
		Hyperlink link3 = new Hyperlink("Text3", "DisplayEvent");
		link3.addClickHandler(this);
		//link.setText("Text");
		
		int row = 0, col = 0;
		iTable.setText(row++, col, "New events");
		iTable.setWidget(row++,col,link);
		iTable.setWidget(row++,col,link2);
		iTable.setWidget(row++,col,link3);
		
		
		
		iPanel.add(iTable);
		
	}

	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		
	}

}
